package org.example.ExperimentTwo;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IPRegex {
    public static void main(String[] args) {
        String ipAddress="";
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入ipAddress：");
        ipAddress=sc.next();
//       正则表达式
        String ipRegex = "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
//        创建模式
        Pattern pattern=Pattern.compile(ipRegex );
//        创建匹配
        Matcher matcher=pattern.matcher(ipAddress);

        if (matcher.matches()) {
            System.out.println("合法的IP地址");
        } else {
            System.out.println("非法的IP地址");
        }
    }
}
